<template>
  <el-dialog title="选择客户" class="customer-select-wrap" :visible.sync="open" width="70%"
             :before-close="handleClose">
    <div class="csw-content">
      <el-form :model="queryParams" ref="cusSelectForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="认证编码" prop="customerCode">
          <el-input
            v-model="queryParams.customerCode"
            placeholder="请输入认证编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="crmCompanyList" height="50vh" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="客户名称" align="center" prop="customerName"/>
        <el-table-column label="客户类型" align="center" prop="customerType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.crm_company_type" :value="scope.row.customerType"/>
          </template>
        </el-table-column>
        <el-table-column label="认证编码" align="center" prop="customerCode"/>
        <el-table-column label="客户行业" align="center" prop="customerField">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.crm_company_field" :value="scope.row.customerField"/>
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center" prop="liaison"/>
        <el-table-column label="联系人手机号" width="140px" align="center" prop="liaisonPhone"/>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSumbitSelect">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import {listCrmCompany} from "@/api/net/crmCompany";

export default {
  name: 'CustomerSelect',
  props: {
    open: Boolean,
    limit: {
      type: Number,
      default() {
        return 1;
      }
    }
  },
  dicts: ['crm_company_type', 'crm_company_field', 'crm_company_process'],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: '',
        customerCode: '',
      },
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      crmCompanyList: [],
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleClose() {
      // 关闭前触发
      this.$emit('close');
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection;
    },
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listCrmCompany(this.queryParams).then(response => {
        this.crmCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("cusSelectForm");
      this.handleQuery();
    },
    handleSumbitSelect() {
      if (this.ids.length != this.limit) {
        this.$modal.msgError("当前只能选择" + this.limit + "条数据");
        return;
      }
      this.$emit('submit', this.ids);
      this.$emit('close');
    }
  }
}

</script>

<style lang="scss" scoped>
.customer-select-wrap {

  .csw-content {

  }

}
</style>
